import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/train/train_other_user_check_recoder_page.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import '../common/common_nodata_widget.dart';
import 'controller/train_user_check_recoder_list_controller.dart';

///班主任管理的培训
class TrainUserCheckRecoderListPage extends StatefulWidget {
  final String tag;

  final int trainId;

  const TrainUserCheckRecoderListPage({
    Key key,
    @required this.tag,
    @required this.trainId,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TrainUserCheckRecoderListPageState();
  }
}

class _TrainUserCheckRecoderListPageState
    extends State<TrainUserCheckRecoderListPage>
    with SingleTickerProviderStateMixin {
  TrainUserCheckRecoderListController _historyController;

  @override
  void initState() {
    super.initState();
    _historyController = Get.put(
      TrainUserCheckRecoderListController(trainId: widget.trainId),
      tag: widget.tag,
    );
    Future.delayed(Duration.zero, () async {
      await _historyController.requestListFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("培训考核列表"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.tag,
        builder: (TrainUserCheckRecoderListController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _historyController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    List<TrainUserCheckRecoderEntity> _list = _historyController.getList();

    if (_list == null || _list.length == 0) {
      return Center(
        child: NoDataWidget(
          text: "暂无管理的培训",
          clickCallBack: () {
            _historyController.requestListFunction(click: true);
          },
        ),
      );
    }
    return RefreshIndicator(
      onRefresh: () async {
        await _historyController.requestListFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: buildListView(),
    );
  }

  AnimatedSwitcher buildListViewAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(
      ///动画执行切换时间
      duration: const Duration(milliseconds: 200),

      ///动画构建器 构建指定动画类型
      transitionBuilder: (Widget child, Animation<double> animation) {
        //执行缩放动画
        return FadeTransition(child: child, opacity: animation);
      },

      ///执行动画的子 Widget
      ///只有子 Widget 被切换时才会触发动画
      child: Container(
        key: UniqueKey(),
        child: buildMyListWidget(),
      ),
    );
  }

  int _currentIndex = 0;

  buildMyListWidget() {
    if (_currentIndex == 0) {
      return buildListView();
    }
    return Padding(
      padding: EdgeInsets.only(left: 10),
    );
  }

  buildListView() {
    List<TrainUserCheckRecoderEntity> _list = _historyController.getList();
    return ListView.builder(
      padding: EdgeInsets.zero,

      physics: BouncingScrollPhysics(),
      //条目个数
      itemCount: _list.length,
      //每个子条目的样式
      itemBuilder: (BuildContext context, int index) {
        TrainUserCheckRecoderEntity trainBean = _list[index];

        int statues = trainBean.statues;
        Color color = Colors.grey;
        String statuesStr = "未考评";
        if (statues == 1) {
          color = Colors.green;
          statuesStr = "已考评";
        }

        return GestureDetector(
          onTap: () async {
            await Get.to(() => TrainOtherUserCheckRecoderPage(
                  tag: trainBean.trainId.toString(),
                  userId: trainBean.userId,
                  trainId: trainBean.trainId,
                ));
          },
          child: Container(
            color: Colors.white,
            margin: EdgeInsets.only(bottom: 10),
            padding: EdgeInsets.all(12),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  children: [
                    Image.asset(
                      "assets/images/3.0x/director_3.png",
                      width: 18,
                      height: 18,
                    ),
                    SizedBox(
                      width: 8,
                    ),
                    Expanded(
                      child: Text(
                        "${trainBean.realName} - ${trainBean.idCard}",
                        style: TextStyle(
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w600),
                      ),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 14,
                    )
                  ],
                ),
                SizedBox(height: 8),
                Row(
                  children: [
                    SizedBox(
                      width: 22,
                    ),
                    Text(
                      "状态：$statuesStr",
                      style: TextStyle(color: color),
                    ),
                  ],
                ),
                buildScoreItemWidget(trainBean),
              ],
            ),
          ),
        );
      },
    );
  }

  buildScoreItemWidget(TrainUserCheckRecoderEntity trainBean) {
    return SizedBox();
  }
}
